package mn.idax.exchange.otc.util;

import mn.idax.exchange.common.UTCTimeUtil;
import mn.idax.exchange.otc.enums.AdEnum;

import java.math.BigDecimal;
import java.util.Random;

/**
 * @Auther: dison
 * @Date: 2018/7/27 17:37
 * @Description:
 */
public class OrderUtils {

    /**
     *
     * 功能描述: 
     *      生成订单号
     * @param:orderQty 订单数量
     * @param:adState 购买方向/广告状态(购买0，出售1)
     * @param:pairCode 币种编号 BTC ETH USDT EOS
     * @return:
     * @auther: dison
     * @date: 2018/7/27 18:29
     */
    public static String createOrderNumber(String orderQty, int adState, String pairCode) {
        if (AdEnum.type_buy.getCode().equals(adState)) {
            adState = 0;
        }
        String pair = null;
        if("BTC".equals(pairCode)){
            pair = "001";
        } else if ("ETH".equals(pairCode)){
            pair = "002";
        } else if ("USDT".equals(pairCode)){
            pair = "003";
        } else {
            pair = "004";
        }
        StringBuffer orderNumber = new StringBuffer();
        // 2.币种编号
//        orderNumber.append(pairCode);
        orderNumber.append(pair);
        // 3.购买方向/广告状态(购买0，出售1)
        orderNumber.append(adState);
        // 4.时间(年月日时分秒)
        orderNumber.append(DateUtils.getDateToStr(DateUtils.getUTCNow(),"yyyyMMddHHmmss"));
        // 5.订单数量
        orderNumber.append(incrementNum(5,orderQty));
        // 6. 随机数
        orderNumber.append(getStringRandom(2));
        return orderNumber.toString();
    }

    /**
     *
     * 功能描述:
     *      生成随机数字和字母,
     * @param:length 生产指定位随机数
     * @return:
     * @auther: dison
     * @date: 2018/7/27 18:30
     */
    public static String getStringRandom(int length) {
        StringBuffer val = new StringBuffer();
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char) (random.nextInt(26) + temp));
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val.append(String.valueOf(random.nextInt(10)));
            }
        }
        return val.toString();
    }

    /**
     *
     * 功能描述:
     *      订单数量
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/7/27 18:13
     */
    public static String incrementNum(int maxLength,String num){
        return String.format("%" + maxLength + "s", num).replace(' ', '0');
    }

    public static void main(String[] args) throws Exception {
        System.out.println(createOrderNumber("0.666",1,"ETH_BTC"));
    }
}
